//
//  Eventi3ViewController.m
//  EuroTripValenciaGuide
//
//  Created by Dario Trisciuoglio on 23/07/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Eventi3ViewController.h"


@implementation Eventi3ViewController

@synthesize lista, filteredListContent;

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = @"Prodotti Apple";
	
	//elementi da visualizzare nella tabella 
	lista = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", 
			 @"iPod Touch", @"iMac", @"iBook", @"MacBook", @"MacBook Pro", @"Mac Pro", 
			 @"PowerBook", nil]; 
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;


	
	// crea la lista filtrata, inizializzandola con il numero di elementi dell'array "lista"
	filteredListContent = [[NSMutableArray alloc] initWithCapacity: [lista count]];
	//inserisce in questa  nuova lista gli elementi della lista originale 
	[filteredListContent addObjectsFromArray: lista];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


//setta il numero di righe della tabella
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	//il numero di righe deve corrispondere al numero di elementi della lista
	return [filteredListContent count];
}


// Setta il contenuto delle varie celle 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"]; 
	
	if (cell == nil){ 
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cellID"] autorelease]; 
		//setta lo stile con cui vengono selezionate le righe 	 
	} 
	//inseriamo nella cella l'elemento della lista corrispondente 
	cell.textLabel.text = [filteredListContent objectAtIndex:indexPath.row];
	return cell; 
} 

// Elimina l'elemento dalla tabella e dalla lista
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
	
	//controlla se l'azione compiuta è un'eliminazione
	if (editingStyle == UITableViewCellEditingStyleDelete) { 
		//elimina l'elemento dalla lista
		[filteredListContent removeObjectAtIndex:indexPath.row];
		//elimina le'elemento dalla tabella
		[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
		
	}
}

// Se selezioniamo una riga appare un pop-up con l'elemento in questione
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	UIAlertView *popUp = [[UIAlertView alloc] initWithTitle:@"Hai selezionato:" message:[filteredListContent objectAtIndex:indexPath.row] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[popUp show];
	[popUp release];
}


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
	
	// per prima cosa azzeriamo l'array della ricerca
	[self.filteredListContent removeAllObjects]; 
	
	// controlliamo se gli elementi della tabella corrispondono alla ricerca
	NSString *cellTitle;
	for (cellTitle in lista){
		NSComparisonResult result = [cellTitle compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
		if (result == NSOrderedSame){
			[filteredListContent addObject:cellTitle];
		}
	}
}

#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    [self filterContentForSearchText:searchString scope:
	 [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{
	
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)saearchBar {
    [self.filteredListContent removeAllObjects];
    [self.filteredListContent addObjectsFromArray: lista];
}


- (void)dealloc {
    [super dealloc];
}

@end